/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.data.manager;

import cz.cvut.fel.taskpro.data.imanager.IObjectManager;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
 * This class implement #{@link ObjectManager} interface
 * @author Tom
 * @version 1.0. beta
 */

public class ObjectManager implements IObjectManager {

    @PersistenceContext(unitName = "SZU-ejbPU")
    protected EntityManager em;

    public ObjectManager() {
        /*
        Handler handler = new ConsoleHandler();
        handler.setFormatter(new SimpleFormatter());
        */
    }

    @Override
    public void add(Object objectToAdd) {
        objectToAdd = em.merge(objectToAdd);
        em.persist(objectToAdd);
    }
   
    @Override
    public void remove(Object objectToRemove) {
        objectToRemove = em.merge(objectToRemove);
        em.remove(objectToRemove);
    }

 
    @Override
    public Object get(Class classToFind, int id) {
        Object o = em.find(classToFind, id);
        return o;
    }
   
    public Object get(Class classToFind, long id) {
        Object o = em.find(classToFind, id);
        return o;
    }

   
    @Override
    public void update(Object objectToUpdate) {
        objectToUpdate = em.merge(objectToUpdate);
        em.persist(objectToUpdate);
       
    }

    @Override
    public Object refresh(Object objectToRefresh) {
        objectToRefresh = em.merge(objectToRefresh);
        em.refresh(objectToRefresh);
        return objectToRefresh;
    }
}
